package cn.donting.jme.swing.tab.scene;

import cn.donting.jme.EditorContent;
import cn.donting.jme.editor.scene.SceneCamera;
import cn.donting.jme.swing.tab.scene.treenode.CameraTreeNode;
import cn.donting.jme.swing.tab.scene.treenode.LightTreeNode;
import cn.donting.jme.swing.tab.scene.treenode.SceneTreeNode;
import cn.donting.jme.swing.tab.scene.treenode.SpatialTreeNode;
import cn.donting.jme.utile.IconUtil;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.jme3.scene.Spatial;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class JDropTree extends JTree {
    private boolean allowSelection = false;
    private ArrayList<TreeSelectionListener> treeSelectionListenerList;

    private ArrayList<ContentMenuListener> contentMenuListeners;

    public JDropTree() {
        super.addTreeSelectionListener(this::treeSelectionEvent);
        setCellRenderer(this::getTreeCellRendererComponent);
        DropMouseAdapter dropMouseAdapter = new DropMouseAdapter();
        addMouseListener(dropMouseAdapter);
        treeSelectionListenerList = new ArrayList();
        contentMenuListeners = new ArrayList();

        setShowsRootHandles(true);
        setDragEnabled(true);
    }

    @Override
    public void addTreeSelectionListener(TreeSelectionListener tsl) {
        treeSelectionListenerList.add(tsl);
    }

    @Override
    public void removeTreeSelectionListener(TreeSelectionListener tsl) {
        treeSelectionListenerList.remove(tsl);
    }

    public void addContentMenuListeners(ContentMenuListener contentMenuListener) {
        contentMenuListeners.add(contentMenuListener);
    }

    public void setSelection(TreePath treePath) {
        allowSelection = true;
        super.setSelectionPath(treePath);
    }

    private void treeSelectionEvent(TreeSelectionEvent e) {
        if (allowSelection) {
            allowSelection = false;
            for (TreeSelectionListener treeSelectionListener : treeSelectionListenerList) {
                treeSelectionListener.valueChanged(e);
            }
            return;
        }
        TreePath oldLeadSelectionPath = e.getOldLeadSelectionPath();
        setSelection(oldLeadSelectionPath);
    }

    private Component getTreeCellRendererComponent(JTree tree, Object value,
                                                   boolean selected, boolean expanded,
                                                   boolean leaf, int row, boolean hasFocus) {
        JLabel label = new JLabel("root");
        if (value instanceof SceneTreeNode) {
            label.setIcon(((SceneTreeNode) value).getIcon(16));
            label.setText(((SceneTreeNode) value).getName());
        }
        return label;
    }

    private class DropMouseAdapter extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 1 && SwingUtilities.isLeftMouseButton(e)) {
                setSelection(getClosestPathForLocation(e.getX(), e.getY()));
            }
            if (SwingUtilities.isRightMouseButton(e)) {
                // 获取右键点击的路径
                TreePath path = getClosestPathForLocation(e.getX(), e.getY());
                if (path != null) {
                    setSelection(path);
                    requestFocus();
                }
                for (ContentMenuListener contentMenuListener : contentMenuListeners) {
                    contentMenuListener.contentMenu(e);
                }
            }
        }
    }

    public static interface ContentMenuListener {
        void contentMenu(MouseEvent event);
    }
}
